{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find All K-Distant Indices in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findKDistantIndices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出数组中的所有 K 近邻下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和两个整数 <code>key</code> 和 <code>k</code> 。<strong>K 近邻下标</strong> 是 <code>nums</code> 中的一个下标 <code>i</code> ，并满足至少存在一个下标 <code>j</code> 使得 <code>|i - j| &lt;= k</code> 且 <code>nums[j] == key</code> 。</p>\n",
    "\n",
    "<p>以列表形式返回按 <strong>递增顺序</strong> 排序的所有 K 近邻下标。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [3,4,9,1,3,9,5], key = 9, k = 1\n",
    "<strong>输出：</strong>[1,2,3,4,5,6]\n",
    "<strong>解释：</strong>因此，<code>nums[2] == key</code> 且 <code>nums[5] == key 。\n",
    "- 对下标 0 ，|0 - 2| &gt; k 且 |0 - 5| &gt; k ，所以不存在 j</code> 使得 <code>|0 - j| &lt;= k</code> 且 <code>nums[j] == key 。所以 0 不是一个 K 近邻下标。\n",
    "- 对下标 1 ，|1 - 2| &lt;= k 且 nums[2] == key ，所以 1 是一个 K 近邻下标。\n",
    "- 对下标 2 ，|2 - 2| &lt;= k 且 nums[2] == key ，所以 2 是一个 K 近邻下标。\n",
    "- 对下标 3 ，|3 - 2| &lt;= k 且 nums[2] == key ，所以 3 是一个 K 近邻下标。\n",
    "- 对下标 4 ，|4 - 5| &lt;= k 且 nums[5] == key ，所以 4 是一个 K 近邻下标。\n",
    "- 对下标 5 ，|5 - 5| &lt;= k 且 nums[5] == key ，所以 5 是一个 K 近邻下标。\n",
    "- 对下标 6 ，|6 - 5| &lt;= k 且 nums[5] == key ，所以 6 是一个 K 近邻下标。\n",
    "</code>因此，按递增顺序返回 [1,2,3,4,5,6] 。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [2,2,2,2,2], key = 2, k = 2\n",
    "<strong>输出：</strong>[0,1,2,3,4]\n",
    "<strong>解释：</strong>对 nums 的所有下标 i ，总存在某个下标 j 使得 |i - j| &lt;= k 且 nums[j] == key ，所以每个下标都是一个 <code>K 近邻下标。</code> \n",
    "因此，返回 [0,1,2,3,4] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "\t<li><code>key</code> 是数组 <code>nums</code> 中的一个整数</li>\n",
    "\t<li><code>1 &lt;= k &lt;= nums.length</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-all-k-distant-indices-in-an-array](https://leetcode.cn/problems/find-all-k-distant-indices-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-all-k-distant-indices-in-an-array](https://leetcode.cn/problems/find-all-k-distant-indices-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,4,9,1,3,9,5]\\n9\\n1', '[2,2,2,2,2]\\n2\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        pos, s, length = [], set(), nums.__len__()\n",
    "        for i, num in enumerate(nums):\n",
    "            if num != key:\n",
    "                continue\n",
    "            # print(\"i={}, num={}, max(0, i-k)={}, min(i+k+1, length)={}\".format(i, num, max(0, i-k), min(i+k+1, length)))\n",
    "            for j in range(max(0, i-k), min(i+k+1, length)):\n",
    "                s.add(j)\n",
    "        ans = list(s)\n",
    "        ans.sort()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        s = set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == key:\n",
    "                low = max(0,i-k)\n",
    "                high = min(len(nums)-1,i+k)\n",
    "                for j in range(low,high+1):\n",
    "                    s.add(j)\n",
    "        res = list(s)\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            if i - k < 0 and key in nums[:i + k + 1] or key in nums[i - k:i + k + 1]:\n",
    "                result.append(i)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = set()\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == key:\n",
    "                for j in range(max(0, i - k), min(i + k + 1, len(nums))):\n",
    "                    ans.add(j)\n",
    "        return sorted(list(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i,v in enumerate(nums):\n",
    "            if v == key:\n",
    "                if len(ans) == 0:\n",
    "                    ans += [i for i in range(max(0,i - k) ,min(i + k + 1,n))]\n",
    "                else:                    \n",
    "                    ans += [i for i in range(max(ans[-1]+1,i - k) ,min(i + k + 1,n))]\n",
    "                if ans[-1] == n - 1:\n",
    "                    return ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        r = 0   # 未被判断过的最小下标\n",
    "        n = len(nums)\n",
    "        for j in range(n):\n",
    "            if nums[j] == key:\n",
    "                l = max(r, j - k)\n",
    "                r = min(n, j + k + 1)\n",
    "                res.extend(range(l, r))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res=set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                if i-k<0:\n",
    "                    b=0\n",
    "                else:\n",
    "                    b=i-k\n",
    "                if i+k+1 >len(nums):\n",
    "                    e=len(nums)\n",
    "                else:\n",
    "                    e=i+k+1\n",
    "                for j in range(b,e):\n",
    "                    if abs(i-j)<=k:\n",
    "                        res.add(j)                \n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "#         ans=set()\n",
    "#         n=len(nums)\n",
    "#         for i in range(n):\n",
    "#             if nums[i]==key:\n",
    "#                 for j in range(max(0,i-k),min(n,i+k+1)):\n",
    "#                     ans.add(j)\n",
    "#         return sorted(list(ans))\n",
    "\n",
    "\n",
    "###官2：\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        r = 0   # 未被判断过的最小下标\n",
    "        n = len(nums)\n",
    "        for j in range(n):\n",
    "            if nums[j] == key:\n",
    "                l = max(r, j - k)\n",
    "                r = min(n - 1, j + k) + 1\n",
    "                for i in range(l, r):\n",
    "                    res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        answer = set()\n",
    "        for index, element in enumerate(nums):\n",
    "            if element == key :\n",
    "                for i in range(index-k,index+k+1):\n",
    "                    if  i >= 0 and i <= len(nums) - 1:\n",
    "                        answer.add(i)\n",
    "        return list(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        index = 0\n",
    "        while index < len(nums):\n",
    "            if nums[index] == key:\n",
    "                after_index = index\n",
    "\n",
    "                for i in range(max(ans[-1] + 1 if ans else 0, index - k), index + 1):\n",
    "                    ans.append(i)\n",
    "                    after_index = i\n",
    "                for i in range(min(index + 1, len(nums)), min(index + k + 1, len(nums))):\n",
    "                    after_index = i\n",
    "                    if nums[i] != key:\n",
    "                        ans.append(i)\n",
    "                    else:\n",
    "                        after_index -= 1\n",
    "                        break\n",
    "                index = after_index\n",
    "            index += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res,l=[],0\n",
    "        n=len(nums)\n",
    "        for j in range(n):\n",
    "            if nums[j]==key:\n",
    "                h=max(j-k,l)\n",
    "                l=min(n-1,j+k)+1\n",
    "                for i in range(h,l):\n",
    "                    res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        nums_len = len(nums)\n",
    "        right = nums_len-1\n",
    "        stack = []\n",
    "        for i in range(right,-1,-1):\n",
    "            if nums[i] == key:\n",
    "                stack.append(i)\n",
    "        for i in range(nums_len):\n",
    "            for v in stack:\n",
    "                if abs(i-v) <=k:\n",
    "                    ans.append(i)\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans,l = set(),len(nums)\n",
    "        for i in range(l):\n",
    "            if nums[i] == key:\n",
    "                for idx in range(max(0,i-k),min(l,i+k+1)):\n",
    "                    ans.add(idx)\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans,l = set(),len(nums)\n",
    "        for i in range(l):\n",
    "            if nums[i] == key:\n",
    "                for idx in range(max(0,i-k),min(l,i+k+1)):\n",
    "                    ans.add(idx)\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ret = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(max(0, i - k), min(i + k + 1, n)):\n",
    "                if key == nums[j]:\n",
    "                    ret.append(i)\n",
    "                    break\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = set()\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(i, n):\n",
    "                if abs(i - j) <= k and (nums[j] == key or nums[i] == key):\n",
    "                    ans.add(i)\n",
    "                    ans.add(j)\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in range(0,len(nums)):\n",
    "            flag=False\n",
    "            for j in range(len(nums)):\n",
    "                if abs(i-j)<=k and nums[j]==key:\n",
    "                    flag=True\n",
    "                    break\n",
    "            if flag:\n",
    "                lst.append(i)\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        stack=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                left=max(0,i-k)\n",
    "                right=min(i+k,len(nums)-1)\n",
    "                for p in range(left,right+1):\n",
    "                    if not stack or stack[-1]<p:\n",
    "                        stack.append(p)\n",
    "        return stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        idx = 0\n",
    "        length = len(nums)\n",
    "        l = []\n",
    "        for i, val in enumerate(nums):\n",
    "            if val == key:\n",
    "                idx = max(idx, i - k)\n",
    "                while idx <= i + k and idx < length:\n",
    "                    l.append(idx)\n",
    "                    idx += 1\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res=set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                for j in range(len(nums)):\n",
    "                    if abs(i-j)<=k:\n",
    "                        res.add(j)                \n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        Res = []\n",
    "        for idx,i in enumerate(nums):\n",
    "            for jdx,j in enumerate(nums):\n",
    "                if abs(idx-jdx) <= k and j == key:Res.append(idx);break\n",
    "        return Res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        last = -1\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == key:\n",
    "                for v in range(max([0, i - k, last + 1]), min([len(nums), i + k + 1])):\n",
    "                    res.append(v)\n",
    "                    last = v\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res=set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                for j in range(len(nums)):\n",
    "                    if abs(i-j)<=k:\n",
    "                        res.add(j)                \n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        # 遍历数对\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums[j] == key and abs(i - j) <= k:\n",
    "                    res.append(i)\n",
    "                    break   # 提前结束以防止重复添加\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if key == nums[i]:\n",
    "                for j in range(max(i - k, 0 if not ans else ans[-1] + 1), min(i + k + 1, n)):\n",
    "                    ans.append(j)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if key == nums[i]:\n",
    "                for j in range(max(i - k, 0 if not ans else ans[-1] + 1), min(i + k + 1, n)):\n",
    "                    ans.append(j)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if (i+k < n and key in nums[i:i+k+1]) or (i-k >= 0 and key in nums[i-k:i+1]) or (i+k >= n and key in nums[i:n]) or (i-k < 0 and key in nums[0:i+1]):\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res=set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                if i-k<0:\n",
    "                    b=0\n",
    "                else:\n",
    "                    b=i-k\n",
    "                if i+k+1 >len(nums):\n",
    "                    e=len(nums)\n",
    "                else:\n",
    "                    e=i+k+1\n",
    "                for j in range(b,e):\n",
    "                    if abs(i-j)<=k:\n",
    "                        res.add(j)                \n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums[j]==key and abs(i-j)<=k:\n",
    "                    res.append(i)\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        key_indices = []  # 存储所有nums[j] == key的下标j\n",
    "        k_distant_indices = set()  # 存储所有满足条件的K近邻下标\n",
    "        \n",
    "        # 找出所有nums[j] == key的下标j\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == key:\n",
    "                key_indices.append(i)\n",
    "        \n",
    "        # 检查每个下标i是否是K近邻下标\n",
    "        for i, num in enumerate(nums):\n",
    "            for key_index in key_indices:\n",
    "                if abs(i - key_index) <= k:\n",
    "                    k_distant_indices.add(i)\n",
    "        \n",
    "        # 返回按递增顺序排序的结果\n",
    "        return sorted(list(k_distant_indices))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = set()\n",
    "        for j in range(len(nums)):\n",
    "            if nums[j] == key:\n",
    "                for i in range(j - k, j + k + 1):\n",
    "                    if 0 <= i < len(nums):\n",
    "                        ans.add(i)\n",
    "        return sorted(list(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        result = []\n",
    "        index_list = [ i for i in range(len(nums)) if nums[i] == key]\n",
    "        i, j = 0, 0\n",
    "        while i < len(nums) and j < len(index_list):\n",
    "            if i - index_list[j] > k:\n",
    "                j += 1\n",
    "            else:\n",
    "                if abs(i-index_list[j]) <= k:\n",
    "                    result.append(i)\n",
    "                i += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res =set()\n",
    "        l = len(nums)\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == key:\n",
    "                res.update(range(max(i-k, 0), min(i+k+1, l)))\n",
    "        return sorted(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        state = list(nums)\n",
    "        for i in range(0,n):\n",
    "            if nums[i] == key:\n",
    "                for j in range(max(0,i-k), min(n,i+k+1)):\n",
    "                    state[j] = key\n",
    "        res = []\n",
    "        for i in range(0,n):\n",
    "            if state[i] == key:\n",
    "                res.append(i)\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        lst = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if abs(i-j)<=k and nums[j] ==key:\n",
    "                    lst.append(i)\n",
    "                    break\n",
    "        return lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in range(0,len(nums)):\n",
    "            flag=False\n",
    "            for j in range(len(nums)):\n",
    "                if abs(i-j)<=k and nums[j]==key:\n",
    "                    flag=True\n",
    "                    break\n",
    "            if flag:\n",
    "                lst.append(i)\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "\n",
    "        res = []\n",
    "        r = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "\n",
    "            if nums[i] == key:\n",
    "                l = max(r, i - k)\n",
    "                r = min(n - 1, i + k) + 1\n",
    "\n",
    "                for j in range(l, r):\n",
    "                    res.append(j)\n",
    "                \n",
    "            \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans=set()\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i]==key:\n",
    "                for j in range(max(0,i-k),min(n,i+k+1)):\n",
    "                    ans.add(j)\n",
    "        return sorted(list(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res =set()\n",
    "        l = len(nums)\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == key:\n",
    "                res.update(range(max(i-k, 0), min(i+k+1, l)))\n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\r\n",
    "        n = len(nums)\r\n",
    "        res = []\r\n",
    "        for i in range(n):\r\n",
    "            for j in range(n):\r\n",
    "                if abs(i - j) <= k and nums[j] == key:\r\n",
    "                    res.append(i)\r\n",
    "                    break\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        inf = [0] * len(nums)\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == key:\n",
    "                for j in range(max(i - k, 0), min(i + k, len(nums) - 1) + 1):\n",
    "                    inf[j] = 1\n",
    "        \n",
    "\n",
    "        res = []\n",
    "\n",
    "        for i in range(len(inf)):\n",
    "            if inf[i] == 1:\n",
    "                res.append(i)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        p = [i for i, x in enumerate(nums) if x == key]\n",
    "        # print(p)\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            j = bisect_left(p, i)\n",
    "            if j > 0 and i - p[j-1] <= k:\n",
    "                ans.append(i)\n",
    "            elif j < len(p) and p[j] - i <= k:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in range(0,len(nums)):\n",
    "            flag=False\n",
    "            for j in range(len(nums)):\n",
    "                if abs(i-j)<=k and nums[j]==key:\n",
    "                    flag=True\n",
    "                    break\n",
    "            if flag:\n",
    "                lst.append(i)\n",
    "        return sorted(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if key == nums[i]:\n",
    "                for j in range(max(i - k, 0 if not ans else ans[-1] + 1), min(i + k + 1, n)):\n",
    "                    ans.append(j)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = [0]*n\n",
    "        pre= -inf\n",
    "        for i in range(n):\n",
    "            if nums[i] == key:\n",
    "                pre =i\n",
    "            if i-pre<=k:\n",
    "                ans[i] = 1\n",
    "\n",
    "        pre = inf\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if nums[i] == key:\n",
    "                pre = i\n",
    "            if pre - i <= k:\n",
    "                ans[i] = 1\n",
    "        return [i for i in range(n) if ans[i]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if (i+k < n and key in nums[i:i+k+1]) or (i-k >= 0 and key in nums[i-k:i+1]) or (i+k >= n and key in nums[i:n]) or (i-k < 0 and key in nums[0:i+1]):\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        r = 0   # 未被判断过的最小下标\n",
    "        n = len(nums)\n",
    "        for j in range(n):\n",
    "            if nums[j] == key:\n",
    "                l = max(r, j - k)\n",
    "                r = min(n - 1, j + k) + 1\n",
    "                for i in range(l, r):\n",
    "                    res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        s = set()\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == key:\n",
    "                s |= set(range(max(0, i-k), min(len(nums), i+k+1)))\n",
    "        return sorted(list(s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = [0] * n\n",
    "        for i in range(n):\n",
    "            if nums[i] == key:\n",
    "                for j in range(k + 1):\n",
    "                    if i + j >= n and i - j < 0:\n",
    "                        break\n",
    "                    # print(i + j, i - j)\n",
    "                    # print(ans)\n",
    "                    if i + j < n:\n",
    "                        ans[i + j] = 1\n",
    "                    if i - j >= 0:\n",
    "                        ans[i - j] = 1\n",
    "        ret = []\n",
    "        for i in range(n):\n",
    "            if ans[i] == 1:\n",
    "                ret.append(i)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = set()\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == key:\n",
    "                for j in range(i-k, i+k+1):\n",
    "                    if 0 <= j < len(nums):\n",
    "                        ans.add(j)\n",
    "        \n",
    "        return sorted(list(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = set()\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == key:\n",
    "                left = max(0, i - k)\n",
    "                right = min(len(nums) - 1, i + k)\n",
    "                for j in range(left, right + 1):\n",
    "                    res.add(j)\n",
    "        return sorted(list(res))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        result = []\n",
    "        j_list = []\n",
    "        for j, num in enumerate(nums):\n",
    "            if num == key:\n",
    "                j_list.append(j)\n",
    "        i = 0\n",
    "        j = i - k - 1\n",
    "        while i < len(nums):\n",
    "            if i - j > k:\n",
    "                if len(j_list) > 0:\n",
    "                    j = j_list.pop(0)\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                if abs(i-j) <= k:\n",
    "                    result.append(i)\n",
    "                i += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        j_indexes = [i for i, x in enumerate(nums) if x == key]\n",
    "        if len(j_indexes) == 0:\n",
    "            return []\n",
    "        answer = []\n",
    "        for i in range(len(nums)):\n",
    "            result_list = [abs(x-i) for x in j_indexes]\n",
    "            if [c for c, x1 in enumerate(result_list) if x1 <= k]:\n",
    "                answer.append(i)\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        s=set()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==key:\n",
    "                s=s|set([j for j in range(max(0,i-k),min(len(nums),i+k+1))])\n",
    "        return sorted(list(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        list1=[]\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(max(0,i-k),min(len(nums),i+k+1)):\n",
    "                if nums[j]==key:\n",
    "                    list1.append(i)\n",
    "        set1=list(set(list1))\n",
    "        set1.sort()\n",
    "        return set1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if abs(i-j)<=k and nums[j]==key:\n",
    "                    res.append(i)\n",
    "        return list(set(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        lst = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if abs(i-j)<=k and nums[j] ==key:\n",
    "                    lst.append(i)\n",
    "        lst = list(set(lst))\n",
    "        lst.sort()\n",
    "        return lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        n, ans = len(nums), []\n",
    "        for j in range(n):\n",
    "            if nums[j] == key:\n",
    "                if j - k >= 0:\n",
    "                    ans += range(j - k, j)\n",
    "                else:\n",
    "                    ans += range(0, j)\n",
    "                if j + k < n:\n",
    "                    ans += range(j, j + k + 1)\n",
    "                else:\n",
    "                    ans += range(j, n)\n",
    "        return sorted(list(set(ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == key:\n",
    "                result.extend(list(range(max(0, i-k), min(i + k + 1, len(nums)))))\n",
    "\n",
    "        return sorted(list(set(result)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        for j in range(len(nums)):\n",
    "            if nums[j] == key:\n",
    "                if j - k >= 0:\n",
    "                    ans += range(j - k, j)\n",
    "                else:\n",
    "                    ans += range(0, j)\n",
    "                if j + k < len(nums):\n",
    "                    ans += range(j, j + k + 1)\n",
    "                else:\n",
    "                    ans += range(j, len(nums))\n",
    "        return sorted(list(set(ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        idx = []\n",
    "        for i,v in enumerate(nums):\n",
    "            if v == key: idx.append(i)\n",
    "        ans = []\n",
    "        for i in idx:\n",
    "            ans += [x for x in range(max(0,i-k),min(len(nums),i+k+1))]\n",
    "        return sorted(list(set(ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        indexList = [-1 for i in range(nums.count(key))]\n",
    "        for i in range (0,nums.count(key)):\n",
    "            if i ==0:\n",
    "                keyID = nums.index(key)\n",
    "                indexList[i] = keyID\n",
    "            else:\n",
    "                keyID = nums.index(key,indexList[i-1]+1)\n",
    "                indexList[i] = keyID\n",
    "        indexListCopy = indexList.copy()\n",
    "        for i in indexListCopy:\n",
    "            for j in range(0,k+1):\n",
    "                if (i+j) < len(nums):\n",
    "                    indexList.append(i+j)\n",
    "            for j in range(-k,0):\n",
    "                if (i+j) >= 0:\n",
    "                    indexList.append(i+j)\n",
    "        se = set(indexList)\n",
    "        return sorted(se)\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == key:\n",
    "                result.extend(list(range(max(0, i-k), min(i + k + 1, len(nums)))))\n",
    "\n",
    "        return sorted(list(set(result)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        indexList = [-1 for i in range(nums.count(key))]\n",
    "        for i in range (0,nums.count(key)):\n",
    "            if i ==0:\n",
    "                keyID = nums.index(key)\n",
    "                indexList[i] = keyID\n",
    "            else:\n",
    "                keyID = nums.index(key,indexList[i-1]+1)\n",
    "                indexList[i] = keyID\n",
    "        indexListCopy = indexList.copy()\n",
    "        for i in indexListCopy:\n",
    "            for j in range(0,k+1):\n",
    "                if (i+j) < len(nums):\n",
    "                    indexList.append(i+j)\n",
    "            for j in range(-k,0):\n",
    "                if (i+j) >= 0:\n",
    "                    indexList.append(i+j)\n",
    "        indexListCopy = list({}.fromkeys(indexList).keys())\n",
    "        indexListCopy.sort()\n",
    "        return indexListCopy\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        ans = []\n",
    "        for x in range(len(nums)):\n",
    "            if nums[x] == key:\n",
    "                if x - k >= 0 and x + k < len(nums):\n",
    "                    ans.extend(list(range(x - k,x + k + 1)))\n",
    "                elif x - k < 0 and x + k < len(nums):\n",
    "                    ans.extend(list(range(0,x + k + 1)))\n",
    "                elif x - k >= 0 and x + k >= len(nums):\n",
    "                    ans.extend(list(range(x - k,len(nums))))\n",
    "                else:\n",
    "                    ans.extend(list(range(0,len(nums))))\n",
    "        return sorted(list(set(ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n",
    "        res=[]\n",
    "        for i,v in enumerate(nums):\n",
    "            if v==key:\n",
    "                if i-k<0:\n",
    "                    _min=0\n",
    "                else:\n",
    "                    _min=i-k\n",
    "                if i+k>=len(nums):\n",
    "                    _max=len(nums)-1\n",
    "                else:\n",
    "                    _max=i+k\n",
    "                res+=range(_min,_max+1)\n",
    "        return list(set(res))\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
